#!/bin/bash
#clear

compile()
{
	cd src/ && make && cd .. 
}

clean()
{ 
	cd src/ && make clean && cd .. 
}

tqemu()
{ 
	qemu -fda floppy.img -m 512 -hda disk.img -boot a
	#qemu -fda floppy.img -d out_asm,in_asm,op,op_opt,int,exec,cpu,pcall,cpu_reset -m 256 
}

tbochs()
{ 
	bochs -f bochs.rc 
}

tinstall()
{ 
	sudo cp src/kernel/kernel /boot/spiderpig 
}

timage()
{
	sudo /sbin/losetup /dev/loop0 floppy.img
	sudo mount /dev/loop0 /mnt/sp
	sudo cp logo/grub.xpm.gz /mnt/sp/
	sudo cp src/menu.lst /mnt/sp/boot/grub
	sudo cp src/kernel/kernel /mnt/sp/kernel
	sudo umount /dev/loop0
	sudo /sbin/losetup -d /dev/loop0
}

uimage()
{ 
	sudo umount /dev/loop0 && sudo /sbin/losetup -d /dev/loop0 
}

floppy()
{ 
	dd if=floppy.img of=/dev/sdb 
}

backup()
{
	mkdir back/$(date -u +%F)
	cp -r src ./do floppy.img bochs.rc back/$(date -u +%F)
}

svnd()
{
	#cd /home/dak/nocopy/users/Dak/Spiderpig/
	#svn rm src do.sh --force
	#svn commit -m "sp"
	#cd /home/dak/Spiderpig
	#cp -r src/ /home/dak/nocopy/users/Dak/Spiderpig/ 
	#cp ./do /home/dak/nocopy/users/Dak/Spiderpig/
	#cd /home/dak/nocopy/users/Dak/Spiderpig/
	#svn add /home/dak/nocopy/users/Dak/Spiderpig/*
	#svn commit -m "sp"

	cd /home/dak/Spiderpig/svn/spiderpigos/
	svn rm * --force
	svn commit -m "sp" 
	cd ..
	cd ..
	mkdir svn/spiderpigos/spfork
	cp -r /home/dak/spfork/src /home/dak/spfork/do /home/dak/spfork/grub /home/dak/spfork/isofiles /home/dak/spfork/doc /home/dak/spfork/image.iso svn/spiderpigos/spfork
	cp -r src/ svn/spiderpigos/
	cp ./do svn/spiderpigos/
	cp ./bochs.rc svn/spiderpigos/
	cp ./README svn/spiderpigos/
	cp ./INSTALL svn/spiderpigos/
	cp ./LICENSE svn/spiderpigos/
	cp floppy.img svn/spiderpigos/
	cd svn/spiderpigos/
	svn add *
	svn commit -m "sp"

	cd /home/dak/Spiderpig
}

all()
{
	clean
	compile
	uimage
	timage
}

usage()
{
	echo usage:
	echo "    " $0 all             : clean, compile, link and test
	echo "    " $0 compile         : compile all .c files
	echo "    " $0 clean           : remove all compiled files
	echo "    " $0 qemu            : try the os with qemu
	echo "    " $0 bochs           : try the os with bochs
	echo "    " $0 install         : install spiderpig on /boot
	echo "    " $0 image           : update image.img
	echo "    " $0 floppy          : write image on floppy
	echo "    " $0 svn             : update svn
	echo "    " $0 backup          : backup on back/
	echo you can also combine multiple commands, example:
	echo "    " $0 clean compile   : clean and compile
}

execmd()
{
		# Invalid arguments
		if [ "$1" = "help" ]
		then
			usage
			return
		fi

		# Do all
		if [ "$1" = "all" ]
		then 
			all
			return
		fi

		if [ "$1" = "svn" ]
		then 
			svnd
			return
		fi

		if [ "$1" = "compile" ]
		then 
			compile
			return
		fi

		if [ "$1" = "clean" ]
		then 
			clean
			return
		fi

		if [ "$1" = "qemu" ]
		then 
			tqemu
			return
		fi

		if [ "$1" = "bochs" ]
		then 
			tbochs
			return
		fi

		if [ "$1" = "install" ]
		then 
			tinstall
			return
		fi

		if [ "$1" = "backup" ]
		then 
			backup
			return
		fi

		if [ "$1" = "image" ]
		then 
			timage
			return
		fi

		if [ "$1" = "floppy" ]
		then 
			floppy
			return 
		fi

		usage
		exit 0
}



#for thing in "$@"
#do
#	if [ "$(thing)" == "floppy" ]
#	then
#	else
#		usage
#		exit 0
#	fi
#done


for thing in "$@"
do
	#echo ${thing}
	execmd ${thing}

	#usage	
	#exit 0
done
# End
